package it.fuesi.synoutils.services.tasks.download;

import android.util.Log;

import it.fuesi.synoutils.adapters.DownloadTasksAdapter;
import it.fuesi.synoutils.model.download.DownloadStationClientParameters;
import it.fuesi.synoutils.model.download.DownloadStationInfoResponse;
import it.fuesi.synoutils.model.download.DownloadTasksListDataObject;
import it.fuesi.synoutils.model.download.DownloadTasksListResponse;
import it.fuesi.synoutils.services.client.DownloadStationClient;
import it.fuesi.synoutils.services.client.ServiceClientFactory;
import it.fuesi.synoutils.services.tasks.base.BaseTask;

/**
 * Created by pascotto on 17/10/14.
 */
public class DownloadTaskList extends BaseTask<DownloadTasksListResponse, DownloadTasksListDataObject, DownloadStationClientParameters> {

    private static final String TAG = DownloadTaskList.class.getName();

    public DownloadTaskList(DownloadTasksAdapter adapter){
        super(adapter);
    }

    private static final String CACHE_KEY = "listTasks";

    @Override
    protected String getCacheKey() {
        return CACHE_KEY;
    }

    @Override
    public DownloadTasksListResponse run(DownloadStationClientParameters params)  throws Exception{

            boolean expireCache = params != null ? params.refreshCache() : false;

            DownloadStationClient client = ServiceClientFactory.getClient(DownloadStationClient.class);

            Log.d(TAG, "Fetching data");
            publishProgress("Loading data from disk station");
            DownloadTasksListResponse resp = client.listTasks();

            if(resp.isSuccess()){
                DownloadStationInfoResponse info = client.info();
                if(info.isSuccess()){
                    resp.getData().setInfo(info.getData());
                }
            }


            return resp;


    }


}
